package org.accp.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.accp.dao.HisroleDao;
import org.accp.pojo.Hisrole;
import org.accp.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@RequestMapping("/role")
@Controller
@CrossOrigin("*")
public class HisroleController {
    @Autowired
    private HisroleDao hisroleDao;


    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<Hisrole>> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Hisrole condition) throws Exception{
        Page<Hisrole> pager = PageHelper.startPage(page,limit);
        List<Hisrole> list = hisroleDao.getConditionList(condition);

        return new ResponseResult<>(pager.getTotal(),list);
    }
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(int roleid) throws Exception{
        hisroleDao.delete(roleid);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public ResponseResult batchDelete(Integer[] roleid) throws Exception{
        hisroleDao.batchDelete(roleid);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/assign")
    @ResponseBody
    public ResponseResult assignPers(Integer roleid, String perids) throws  Exception{
        String[] array = perids.split(",");
        List<Integer> ids = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            ids.add(Integer.parseInt(array[i]));
        }
        hisroleDao.assignRights(roleid,ids);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(Hisrole hisrole) throws Exception{
        hisroleDao.add(hisrole);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/update")
    @ResponseBody
    public ResponseResult update(Hisrole hisrole ) throws Exception{
        hisroleDao.update(hisrole);
        return ResponseResult.createSuccessResult();
    }


}